package patterns;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class ComparatorTest {

    public static void main(String[] args) {
        List<Person> personList = Arrays.asList(
                new Person("Mike", 45),
                new Person("Sara", 21),
                new Person("Elsa", 34));
        //age
        Collections.sort(personList, new Comparator <Person> () {
            @Override
            public int compare (Person p0, Person p1) {
                return p0.getAge() - p1.getAge();
            }
        });
        System.out.println(personList);
        
        //name
        Collections.sort(personList, new Comparator<Person> () {
            @Override
            public int compare (Person p0, Person p1){
                return p0.getName().compareTo(p1.getName());
            }
        });
        System.out.println(personList);
    }
}

class Person {

    private String name;
    private int age;

    Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    @Override
    public String toString() {
        return "Person["
                + "name='" + name + '\''
                + ", age=" + age + ']';
    }
}